<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
<head>
<%@ include file="/extjs/include/all.jsp"%>
<link href="${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/userModule/page/css/authority.css" rel="stylesheet" type="text/css">
<title>添加机构</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/validate.js">
</script>
</head>
<body>
<div id="deptInsert"></div>
</body>
</html>
<script>
var userName = '${__SATURN_AUTORITY_USER__.username}';
var userType = '${__SATURN_AUTORITY_USER__.usertype}';
Ext.onReady(function(){

	Ext.QuickTips.init();  

	var deptForm = new Ext.FormPanel({
		labelWidth: 75,
	    id:'deptForm',
	    frame:true,
	    buttonAlign:'right', 
	    bodyStyle:'padding:5px 5px 0',
	    width: 350,
	    defaults: {width: 230},
	    defaultType: 'textfield',
	    items: [{
	        	fieldLabel: '机构全码',
	            name: 'code_show',
	            id: 'code_show',
	            disabled:true
	        },{
	        	fieldLabel: '机构全码',
	            name: 'code',
	            id: 'code',
	            inputType:'hidden',
	            hidden:true 
	        },{
	            fieldLabel: '机构ID',
	            name: 'id',
	            id: 'id',
	            listeners:{"blur":setCode},
	            maxLength:10,
	            vtype:'isNumber',
	            allowBlank:false,
	            blankText:'机构ID不可为空'
	        },{
	            fieldLabel: '机构名称',
	            name: 'name',
	            id: 'name',
	            maxLength:20,
	            allowBlank:false,
	            blankText:'机构名称不可为空',
	            vtype: 'isNumberAlphaUnderlineChinese'
	        }, {
	        	xtype:'textarea',
	        	fieldLabel: '描述',
	            name: 'descn',
	            id: 'descn',
	            vtype: 'isNumberAlphaUnderlineChineseSpaceMaxLength200'
	        },{
	            fieldLabel: '父机构',
	            name: 'parent_id_show',
	            id: 'parent_id_show',
	            disabled:true
	        },{
	            fieldLabel: '父机构',
	            name: 'parent_id',
	            id: 'parent_id',
	            inputType:'hidden',
	            hidden:true 
	        },{
	            fieldLabel: '父机构名称',
	            name: 'parent_name_show',
	            id: 'parent_name_show',
	            disabled:true
	        },{
	            fieldLabel: '父机构名称',
	            name: 'parent_name',
	            id: 'parent_name',
	            inputType:'hidden',
	            hidden:true 
	        }
	    ],

        buttons: [{
            text: '确认',
            handler: formSubmit 
        },{
            text: '返回',
            handler: function(){
				window.location = 'deptEdit.jsp?deptId=<%=request.getParameter("id")%>';
			}
        }]
	});
	deptForm.render('deptInsert');
	var initValue;
	Ext.Ajax.request({
		method:'POST',
		url:'${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/departmentModule/action/QueryforAddDept.action',
		success:function(request){                   //发送成功的回调函数
	       	var myResult = Ext.util.JSON.decode(request.responseText);
	       	Ext.get("code").dom.value = myResult.parentDept.code;
	       	Ext.get("parent_id").dom.value = myResult.parentDept.id;
	       	Ext.get("parent_name").dom.value = myResult.parentDept.name;
	       	Ext.get("code_show").dom.value = myResult.parentDept.code;
	       	Ext.get("parent_id_show").dom.value = myResult.parentDept.id;
	       	Ext.get("parent_name_show").dom.value = myResult.parentDept.name;
	       	initValue = Ext.get("code").dom.value;
	     },
	     failure:function(){                          //发送失败的回调函数
	       	Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
	     },
	     params:{
	     	id:'<%=request.getParameter("id")%>'
	     } 
	});

	function setCode(){
		Ext.get("code").dom.value = Ext.get("id").dom.value + "." +initValue;	
		Ext.get("code_show").dom.value = Ext.get("code").dom.value;
	}
	
	function formSubmit(){
		if(deptForm.form.isValid()){
			deptForm.form.submit({
				url:'${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/departmentModule/action/AddDept.action',
				success:function(form, action){
					var myResult = Ext.util.JSON.decode(action.response.responseText);
					if(myResult.isAccept == 'true'){
						deptForm.form.submit({
							url:'${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/departmentModule/action/TrueAddDept.action',
							success:function(form, action){
								var myResult = Ext.util.JSON.decode(action.response.responseText);	
								Ext.MessageBox.alert('提示', '添加成功!', 
										function(){
											//var newNode = new Ext.tree.TreeNode({
											//	text:Ext.get("name").dom.value,
											//	deptId:Ext.get("id").dom.value
											//});
											window.parent.refreshTree(Ext.get("parent_id").dom.value);
										});
							},
							failure:function(form, action){
								Ext.MessageBox.alert('警告','与后台联系发生错误!');
							}
						})
					} else {
						Ext.MessageBox.alert('提示','此机构已经存在,不可添加!');
					}
				},
				failure:function(form, action){
					Ext.MessageBox.alert('警告','与后台联系发生错误!');
				}
			})
		}
	}
	document.getElementById('name').maxLength = 20;
	document.getElementById('id').maxLength = 10;
});

</script>